public class DistributorPlaceAnOrderController
{
   public String RecordId
   {
       get
       {
           return String.valueOf(System.currentPageReference().getParameters().get('id')).substring(0,15);  
       }
       set;
   }
   public Order__c order;
   
    public PageReference ConfirmPlaceOrder()
    {
         order=[select id,DistributorConfirm__c,Outlet_name__r.CityName__c from Order__c where id=:RecordId];
        if(!order.DistributorConfirm__c)
        {
             ModifyActivityTotalNumberAndOutletNumber();
             order.DistributorConfirm__c=true;
             update order;
        } 
         
        pageReference pr = new pageReference('/'+RecordId);     
        pr.setredirect(true);   
        return pr; 
    }
    
      public void ModifyActivityTotalNumberAndOutletNumber()
      {
            List<Order_Item__c> orderList= [select id,name,Product_Name__c,OrderNumber__c,Unit__c,Package__c,DistributorSetOrderNumber__c,Resource_Allocation_Detail__c  
                                                               from Order_Item__c where Order_Name__c=:RecordId];
           //取得产品ID
         //  map<Id,Order_Item__c> productIdList=new map<Id,Order_Item__c>();
            set<string> resourceIdList=new set<string>();
           for(Order_Item__c oItem:orderList)
           {
               resourceIdList.add(oItem.Resource_Allocation_Detail__c);
           }
          map<id,Resource_Allocation_Detail__c> radList=new map<id,Resource_Allocation_Detail__c>([select id,ProductId__c,
                                                                                                                                                          HasUsedResourceNumber__c,Resource_Quantity_Number__c
                                                                                                                                                          from Resource_Allocation_Detail__c 
                                                                                                                                                          where id in:resourceIdList]);
            map<string,Order_Item__c> productIdList=new map<string,Order_Item__c>();                                                                                                                                              
           for(Order_Item__c oItem:orderList)
           {
               productIdList.put(oItem.Product_Name__c,oItem);
               if(radList.containsKey(oItem.Resource_Allocation_Detail__c))
               {
                   Resource_Allocation_Detail__c rad=radList.get(oItem.Resource_Allocation_Detail__c);
                    if(oItem.DistributorSetOrderNumber__c<oItem.OrderNumber__c)
                    {
                         rad.HasUsedResourceNumber__c-=oItem.OrderNumber__c-oItem.DistributorSetOrderNumber__c;
                         radList.put(oItem.Resource_Allocation_Detail__c,rad);
                    }
               }
           }
           update radList.values();
           
           //插入到出库表
           //找出shipment city
           List<Delivery__c> deliveryList=new List<Delivery__c>();
          ShipmentCity__c sCity= Database.query('select id from ShipmentCity__c where SalesCity__c includes (\''+order.Outlet_name__r.CityName__c+'\') and IsTrade__c=true limit 1');
          for(CityInventory__c cInventory:[select id,Product__c from CityInventory__c 
                                                              where ShipmentCity__c=:sCity.id 
                                                              and Product__c in:productIdList.keySet()])
          {
              if(productIdList.containsKey(cInventory.Product__c))
              {
                  Order_Item__c ot=productIdList.get(cInventory.Product__c);
                  Delivery__c outProduct=new Delivery__c();
                  outProduct.CityInventory__c=cInventory.id;
                  outProduct.Type__c='Order';
                  outProduct.Unit__c=ot.DistributorSetOrderNumber__c;
                  outProduct.Product__c=ot.Product_Name__c;
                  deliveryList.add(outProduct);
              }
          }
          if(deliveryList.size()>0)
              insert deliveryList;
       }
}